home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Toolbox / Visual Basic Toolbox (P.I.E.)(1996).ISO / forms / frmwiz / rdblib / rdblib.bas < prev    next >
BASIC Source File  |  1995-01-02  |  16KB  |  417 lines

  1. ' Common Subroutine & Functions Module
  2. ' Provided by:
  3. '    Royce D. Bacon
  4. '    RDB Systems
  5. '    8942 W. Lawrence Ave.
  6. '    Milwaukee, WI  53225
  7. '    Compuserve ID: 70042,1001
  8. '
  9. ' You may use these routines in your own programs and
  10. ' distribute them or the compiled versions of them
  11. ' with your programs.  However, you may not distribute
  12. ' these routines alone for profit.
  13. '
  14. ' Payment for these routines is not required, but will
  15. ' always be appreciated.
  16. '
  17.  
  18.  
  19. Global rb_systemname As String
  20. Global rb_version As String
  21. Global RB_Erraction As Integer
  22. Global Const RB_GRAY = &HC0C0C0
  23. ' Constants, etc. for screen capture/print function
  24. Global Const SW_HIDE = 0
  25. Global Const SW_SHOW = 5
  26. Declare Function ShowWindow Lib "User" (ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer
  27.  
  28.  
  29. ' Windows function declarations
  30.  
  31. Declare Function GetModuleUsage Lib "KERNEL" (ByVal InstanceID%) As Integer
  32.  
  33. '******************************************************
  34. '           DLL Declarations                          *
  35. '******************************************************
  36. Type POINTAPI
  37.     X As Integer
  38.     Y As Integer
  39. End Type
  40.  
  41. Declare Function LoadMenu Lib "User" (ByVal hInstance As Integer, ByVal lpString As String) As Integer
  42. Declare Function GetMenu Lib "User" (ByVal hwnd As Integer) As Integer
  43. Declare Function SetMenu Lib "User" (ByVal hwnd As Integer, ByVal hMenu As Integer) As Integer
  44. Declare Function HiliteMenuItem Lib "User" (ByVal hwnd As Integer, ByVal hMenu As Integer, ByVal wIDHiliteItem As Integer, ByVal wHilite As Integer) As Integer
  45. Declare Function GetMenuString Lib "User" (ByVal hMenu As Integer, ByVal wIDItem As Integer, ByVal lpString As String, ByVal nMaxCount As Integer, ByVal wFlag As Integer) As Integer
  46. Declare Function GetMenuState Lib "User" (ByVal hMenu As Integer, ByVal wId As Integer, ByVal wFlags As Integer) As Integer
  47. Declare Sub DrawMenuBar Lib "User" (ByVal hwnd As Integer)
  48. Declare Function GetSystemMenu Lib "User" (ByVal hwnd As Integer, ByVal bRevert As Integer) As Integer
  49. Declare Function GetSubMenu Lib "User" (ByVal hMenu As Integer, ByVal nPos As Integer) As Integer
  50. Declare Function GetMenuItemID Lib "User" (ByVal hMenu As Integer, ByVal nPos As Integer) As Integer
  51. Declare Function GetMenuItemCount Lib "User" (ByVal hMenu As Integer) As Integer
  52. Declare Function TrackPopupMenu Lib "User" (ByVal hMenu As Integer, ByVal wFlags As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal nReserved As Integer, ByVal hwnd As Integer, lpReserved As Any) As Integer
  53. Declare Function InsertMenu Lib "User" (ByVal hMenu As Integer, ByVal nPosition As Integer, ByVal wFlags As Integer, ByVal wIDNewItem As Integer, ByVal lpNewItem As Any) As Integer
  54. Declare Function AppendMenu Lib "User" (ByVal hMenu As Integer, ByVal wFlags As Integer, ByVal wIDNewItem As Integer, ByVal lpNewItem As Any) As Integer
  55. Declare Function ModifyMenu Lib "User" (ByVal hMenu As Integer, ByVal nPosition As Integer, ByVal wFlags As Integer, ByVal wIDNewItem As Integer, ByVal lpString As Any) As Integer
  56. Declare Function RemoveMenu Lib "User" (ByVal hMenu As Integer, ByVal nPosition As Integer, ByVal wFlags As Integer) As Integer
  57. Declare Function DeleteMenu Lib "User" (ByVal hMenu As Integer, ByVal nPosition As Integer, ByVal wFlags As Integer) As Integer
  58.  
  59. Declare Function ExitWindows Lib "User" (ByVal dwReserved As Long, wReturnCode) As Integer
  60. Declare Function GetDeviceCaps Lib "GDI" (ByVal hDC As Integer, ByVal nIndex As Integer) As Integer
  61. Declare Function GetActiveWindow Lib "User" () As Integer
  62. Declare Sub GetCursorPos Lib "User" (lpPoint As POINTAPI)
  63. Declare Function GetSystemMetrics Lib "User" (ByVal nIndex As Integer) As Integer
  64. Declare Function GetFocus Lib "User" () As Integer
  65. Declare Function SetActiveWindow Lib "User" (ByVal hwnd As Integer) As Integer
  66. Declare Function GetModuleHandle Lib "Kernel" (ByVal lpModuleName As String) As Integer
  67. Declare Function GetModuleFileName Lib "Kernel" (ByVal hModule As Integer, ByVal lpFilename As String, ByVal nSize As Integer) As Integer
  68. Declare Function GetFreeSpace Lib "Kernel" (ByVal wFlags As Integer) As Long
  69.  
  70. 'Indices for GetSystemMetrics
  71. Global Const SM_CXSIZE = 30
  72. Global Const SM_CYSIZE = 31
  73.  
  74. 'Indices for GetDeviceCaps
  75. Global Const HORZRES = 8    '  Horizontal width in pixels
  76. Global Const VERTRES = 10   '  Vertical width in pixels
  77.  
  78. 'Menu flags for Add/Check/EnableMenuItem()
  79. Global Const MF_INSERT = &H0
  80. Global Const MF_CHANGE = &H80
  81. Global Const MF_APPEND = &H100
  82. Global Const MF_DELETE = &H200
  83. Global Const MF_REMOVE = &H1000
  84.  
  85. Global Const MF_BYCOMMAND = &H0
  86. Global Const MF_BYPOSITION = &H400
  87.  
  88. Global Const MF_SEPARATOR = &H800
  89.  
  90. Global Const MF_ENABLED = &H0
  91. Global Const MF_GRAYED = &H1
  92. Global Const MF_DISABLED = &H2
  93.  
  94. Global Const MF_UNCHECKED = &H0
  95. Global Const MF_CHECKED = &H8
  96. Global Const MF_USECHECKBITMAPS = &H200
  97.  
  98. Global Const MF_STRING = &H0
  99. Global Const MF_BITMAP = &H4
  100. Global Const MF_OWNERDRAW = &H100
  101.  
  102. Global Const MF_POPUP = &H10
  103. Global Const MF_MENUBARBREAK = &H20
  104. Global Const MF_MENUBREAK = &H40
  105.  
  106. Global Const MF_UNHILITE = &H0
  107. Global Const MF_HILITE = &H80
  108.  
  109. Global Const MF_SYSMENU = &H2000
  110. Global Const MF_HELP = &H4000
  111. Global Const MF_MOUSESELECT = &H8000
  112.  
  113. '  Menu item resource format
  114. Type MENUITEMTEMPLATEHEADER
  115.     versionNumber As Integer
  116.     offset As Integer
  117. End Type
  118.  
  119. Type MENUITEMTEMPLATE
  120.     mtOption As Integer
  121.     mtID As Integer
  122.     mtString As Long
  123. End Type
  124.  
  125. Global Const MF_END = &H80
  126.  
  127. '  System Menu Command Values
  128. Global Const SC_SIZE = &HF000
  129. Global Const SC_MOVE = &HF010
  130. Global Const SC_MINIMIZE = &HF020
  131. Global Const SC_MAXIMIZE = &HF030
  132. Global Const SC_NEXTWINDOW = &HF040
  133. Global Const SC_PREVWINDOW = &HF050
  134. Global Const SC_CLOSE = &HF060
  135. Global Const SC_VSCROLL = &HF070
  136. Global Const SC_HSCROLL = &HF080
  137. Global Const SC_MOUSEMENU = &HF090
  138. Global Const SC_KEYMENU = &HF100
  139. Global Const SC_ARRANGE = &HF110
  140. Global Const SC_RESTORE = &HF120
  141. Global Const SC_TASKLIST = &HF130
  142.  
  143. '******************************************************
  144. '*          OpenFile Modes                            *
  145. '******************************************************
  146. Global Const REPLACEFILE = 0
  147. Global Const READFILE = 1
  148. Global Const APPENDFILE = 2
  149. Global Const RANDOMFILE = 3
  150. Global Const BINARYFILE = 4
  151.  
  152.  
  153. '**************************************************
  154. ' Declares for screen grabber function
  155. '**************************************************
  156. Type lrect
  157.     Left As Integer
  158.     Top As Integer
  159.  
  160.     right As Integer
  161.     bottom As Integer
  162. End Type
  163. Declare Function GetDesktopWindow Lib "user" () As Integer
  164. Declare Function GetDC Lib "user" (ByVal hwnd%) As Integer
  165.  
  166. ' Note: The following Declare should be on one line:
  167. Declare Function BitBlt Lib "GDI" (ByVal hDestDC%, ByVal X%, ByVal Y%, ByVal nWidth%, ByVal nHeight%, ByVal hSrcDC%, ByVal XSrc%, ByVal YSrc%, ByVal dwRop&) As Integer
  168. Declare Function ReleaseDC Lib "User" (ByVal hwnd As Integer, ByVal hDC As Integer) As Integer
  169.  
  170. Declare Sub GetWindowRect Lib "User" (ByVal hwnd%, lpRect As lrect)
  171. Global TwipsPerPixel As Single
  172.  
  173.  
  174. 'Other API Declarations For Sound
  175. Declare Sub MessageBeep Lib "User" (ByVal wType As Integer)
  176. Declare Sub SndPlaySound Lib "MMSystem.dll" (ByVal WavFile$, ByVal wFlags As Integer)
  177.  
  178. Sub Form3D (formname As Form)
  179.    ' This code came from Visual Basic Tips And Techniques 94
  180.    ' Tip Submitted By: Matej Nastran
  181.    ' Modified to set 3-D based upon control type instead of tag = 3-D
  182.    Dim drkgray As Long, fullwhite As Long
  183.    Dim i As Integer, dw As Integer, Do3D As Integer
  184.    Dim ctop As Integer, cleft As Integer, cright As Integer, cbottom As Integer
  185.  
  186.     ' Outline a form's text and combobox controls for 3D look
  187.  
  188.     Dim cname As Control
  189.  
  190.     drkgray = RGB(128, 128, 128)
  191.     fullwhite = RGB(255, 255, 255)
  192.  
  193.     dw = formname.DrawWidth
  194.     formname.DrawWidth = 1      'this suits me best
  195.     For i = 0 To (formname.Controls.Count - 1)
  196.         Set cname = formname.Controls(i)
  197.         If TypeOf cname Is TextBox Then
  198.             Do3D = True
  199.         ElseIf TypeOf cname Is ComboBox Then
  200.             Do3D = True
  201.         Else
  202.             Do3D = False
  203.         End If
  204.         If Do3D Then
  205.             ctop = cname.Top - Screen.TwipsPerPixelY
  206.             cleft = cname.Left - Screen.TwipsPerPixelX
  207.             cright = cname.Left + cname.Width
  208.             cbottom = cname.Top + cname.Height
  209.             formname.Line (cleft, ctop)-(cright, ctop), drkgray
  210.             formname.Line (cleft, ctop)-(cleft, cbottom), drkgray
  211.             formname.Line (cleft, cbottom)-(cright, cbottom), fullwhite
  212.             formname.Line (cright, ctop)-(cright, cbottom), fullwhite
  213.         End If
  214.     Next i
  215.     formname.DrawWidth = dw
  216. End Sub
  217.  
  218. Sub RB_Center (str_to_print As String, line_no, skip_line As Integer)
  219.     ' ============= RB_Center ==============================
  220.     ' Will center a string passed as parameter 1
  221.     ' on printer line passed as parameter 2 or current line if parameter 2 = 0
  222.     ' Will skip to next line if parameter 3 = true
  223.     ' e.g. RB_Center "This String Will Be Centered On Line 3", 3, true
  224.     '
  225.     Dim col_to_print_at As Single
  226.     col_to_print_at = ((printer.ScaleWidth - printer.TextWidth(str_to_print)) / 2) + printer.ScaleLeft
  227.     printer.CurrentX = col_to_print_at
  228.     If line_no <> 0 Then
  229.         printer.CurrentY = line_no
  230.     End If
  231.     If skip_line Then
  232.         printer.Print str_to_print
  233.     Else
  234.         printer.Print str_to_print;
  235.     End If
  236.  
  237. End Sub
  238.  
  239. Function RB_ErrorHandler (pform As String, proutine As String) As Integer
  240.     ' =================== RB_ErrorHandler =========================
  241.     ' Displays dialog indicating error and allows user to
  242.     ' print problem report form, obtain help on error condition,
  243.     ' abort program, retry the function, or ignore the error
  244.     '
  245.     ' Example of using RB_ErrorHandler
  246.     ' erraction = RB_ErrorHandler("FormName", "Routine")
  247.     ' Select Case erraction
  248.     ' Case 1
  249.     '     Resume 0      ' Retry option selected
  250.     ' Case 2
  251.     '     Resume Next   ' Ignore option selected
  252.     ' End Select
  253.     '
  254.     ' To use in your projects include RDBLIB.BAS, RBERRFRM.FRM,
  255.     ' RBPROBRP.FRM, RBSCRN.FRM
  256.     
  257.     Dim RB_err As Integer
  258.     Dim RB_error As String
  259.     Dim RB_errl As Long
  260.     Dim RB_Msg As String
  261.     RB_err = Err
  262.     RB_error = Error$
  263.     RB_errl = Erl
  264.     SndPlaySound "crash.wav", 2
  265.     Beep
  266.     RB_Msg = "A " & RB_error & " error (" & RB_err & ") has occurred"
  267.     If RB_errl <> 0 Then
  268.         RB_Msg = RB_Msg & " at line " & RB_errl
  269.     End If
  270.     RB_Msg = RB_Msg + " in routine " & proutine & " of form " & pform
  271.     RB_Msg = RB_Msg & "."
  272.     If RB_err = 3051 Then
  273.         RB_Msg = RB_Msg & "  This error is usually caused because another user on the network, "
  274.         RB_Msg = RB_Msg & "another function on this workstation, is performing a function that "
  275.         RB_Msg = RB_Msg & "requires exclusive use of the indicated file."
  276.     End If
  277.     RBErrFrm.Msg.Text = RB_Msg
  278.     RBErrFrm.SvErr.Caption = RB_err
  279.     RBErrFrm.Show MODAL
  280.     Select Case RB_Erraction
  281.     Case 0
  282.         End
  283.     Case 1
  284.         RB_ErrorHandler = RB_Erraction
  285.     Case 2
  286.         RB_ErrorHandler = RB_Erraction
  287.     End Select
  288.  
  289. End Function
  290.  
  291. Function RB_Rjustify (pnumber, pformat As String, pcol) As Single
  292.     ' ========================= RB_Rjustify ====================
  293.     ' Will print a number passed as parameter 1
  294.     ' according to the format passed as parameter 2
  295.     ' right justified on the column passed as parameter 3
  296.     ' Returns the leftmost column position where printing started
  297.     '
  298.     ' Example:
  299.     ' leftcol = RB_Rjustify(200, "###,###.##", 40)
  300.     ' will print "    200.00" with the rightmost 0 at column 40
  301.     '
  302.     Dim rbpos As Single
  303.     Dim rbstr As String
  304.     Dim rblen As Single
  305.     rbstr = Format$(pnumber, pformat)
  306.     rblen = printer.TextWidth(rbstr)
  307.     rbpos = pcol - rblen
  308.     printer.CurrentX = rbpos
  309.     printer.Print rbstr;
  310.     RB_Rjustify = rbpos
  311.  
  312. End Function
  313.  
  314. Function RB_Text_Format (instring As String, pwidth As Long)
  315.     ' ==================== RB_Text_Format ===================
  316.     ' Will return a string variable passed as parameter 1
  317.     ' formatted to print with a line length of parameter 2
  318.     ' It will break each line at the end of a word
  319.     '
  320.     ' Example:
  321.     ' newstring = RB_Text_Format(oldstring, 65)
  322.     ' Printer.Print newstring
  323.     ' will print the contents of oldstring as 65 character lines
  324.     '
  325.     Dim startpos As Integer, nextrtn As Integer, nextspace As Integer
  326.     Dim svstatpos As Integer, svwkstring As String, wkinstring As String
  327.     Dim wkstring As String, outstring As String, gotstring As Integer
  328.     outstring = ""
  329.     wkinstring = Trim$(instring)
  330.     nextrtn = 0
  331.     startpos = 1
  332.     Do While startpos < Len(wkinstring)
  333.         gotstring = False
  334.         nextrtn = InStr(startpos, wkinstring, Chr$(13))
  335.         If nextrtn > 0 Then
  336.             wkstring = Mid$(wkinstring, startpos, nextrtn - startpos + 1)
  337.             ' Check for string less than 400 characters because long
  338.             ' strings cause an overflow error and definitely won't fit
  339.             ' on a single line
  340.             If Len(wkstring) < 400 Then
  341.                 If printer.TextWidth(wkstring) < pwidth Then
  342.                     outstring = outstring + wkstring
  343.                     startpos = nextrtn + 2
  344.                     gotstring = True
  345.                 End If
  346.             End If
  347.         End If
  348.         If Not gotstring Then
  349.             wkstring = ""
  350.             Do
  351.                 svwkstring = wkstring
  352.                 svstartpos = startpos
  353.                 nextrtn = InStr(startpos, wkinstring, " ")
  354.                 If nextrtn = 0 Then
  355.                     wkstring = wkstring + Mid$(wkinstring, startpos)
  356.                     svwkstring = wkstring
  357.                     startpos = Len(wkinstring) + 1
  358.                     svstartpos = startpos
  359.                 Else
  360.                     wkstring = wkstring + Mid$(wkinstring, startpos, nextrtn - startpos + 1)
  361.                     startpos = nextrtn + 1
  362.                 End If
  363.             Loop While printer.TextWidth(wkstring) <= pwidth And startpos <= Len(wkinstring)
  364.             startpos = svstartpos
  365.             outstring = outstring + svwkstring + Chr$(13) + Chr$(10)
  366.         End If
  367.     Loop
  368.     RB_Text_Format = outstring
  369.  
  370.  
  371. End Function
  372.  
  373. Function RB_Validate_Date (cdate As Control) As Integer
  374.     ' ================= RB_Validate_Date =====================
  375.     ' validates date contained in control passed as parameter 1
  376.     ' will return True if input is valid date, the string "__/__/__" or null
  377.     ' will display a msgbox with an "Enter a valid data" msg and return False
  378.     '      if the input date is invalid
  379.     '
  380.     ' Example:
  381.     ' TxtDate_LostFocus
  382.     '   IF Not RB_Validate_Date(TxtDate) then
  383.     '       Date.setfocus
  384.     '   End If
  385.     '
  386.     Dim wk_date As String
  387.     wk_date = cdate.Text
  388.     If wk_date = "__/__/__" Or wk_date = "" Then
  389.         RB_Validate_Date = True
  390.         cdate.Text = ""
  391.     ElseIf Not IsDate(wk_date) Then
  392.         Beep
  393.         MsgBox "Enter a valid date", , "Date Entry Error"
  394.         RB_Validate_Date = False
  395.     Else
  396.         RB_Validate_Date = True
  397.     End If
  398.  
  399. End Function
  400.  
  401. Sub ShellAndWait (CommandString$)
  402.   ' ============== ShellAndWait =====================
  403.   ' Will start (via Shell Function) the command passed as parameter 1
  404.   ' and wait until the command has completed and the window closed
  405.   '
  406.   ' Example:
  407.   ' ShellAndWait("COPY A.TXT B.TXT")
  408.   ' B.TXT will be available now
  409.   '
  410.   ID% = Shell(CommandString$, 3)
  411.   Do
  412.     X% = DoEvents()
  413.   Loop Until GetModuleUsage(ID%) = 0
  414.  
  415. End Sub
  416.  
  417.